package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
 * 预约设置
 * */

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            //遍历集合
            for (OrderSetting orderSetting : orderSettingList) {
                //根据日期查询，判断当前日期是否已经存在了预约设置
                long count = orderSettingDao.findCountByDate(orderSetting.getOrderDate());
                if (count>0){
                    //如果存在了，修改预约设置
                    orderSettingDao.updateByDate(orderSetting);
                }else {
                    //不存在，插入预约设置
                    orderSettingDao.add(orderSetting);
                }
            }

        }


    }

    //根据年月查询预约设置数据
    @Override
    public List<Map> getOrderSetting(String date) {
        //拼接成yyyy-MM-dd的形式
        String start = date+"-1";
        String end = date+"-31";

        Map<String,String> map=new HashMap<>();
        map.put("start",start);
        map.put("end",end);
        List<OrderSetting> list=orderSettingDao.getOrderSetting(map);
        List<Map> mapList = new ArrayList<>();
        if (list !=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                Map<String ,Object> map1=new HashMap<>();
                map1.put("date",orderSetting.getOrderDate().getDate());//获取日期的号数
                map1.put("number",orderSetting.getNumber());
                map1.put("reservations",orderSetting.getReservations());
                mapList.add(map1);
            }
        }
        return mapList;
    }

    //根据日期对可预约人数进行设置
    @Override
    public void editNumber(OrderSetting orderSetting) {
        //判断是否预约
        long count = orderSettingDao.findCountByDate(orderSetting.getOrderDate());
       if (count>0){
            //进行修改可预约人数
           orderSettingDao.updateByDate(orderSetting);
       }else {
           //不存在，插入预约设置
           orderSettingDao.add(orderSetting);
       }

    }
}
